package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JButton;

import javax.swing.WindowConstants;

import model.basics.Request;

import org.jdesktop.application.Application;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextPane;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SingleTFPanel extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JButton datePickerButton;
	private JButton okButton;
	private JLabel jLabel7;
	private JLabel jLabel6;
	private JTextPane minsToPane;
	private JTextPane hoursToPane;
	private JLabel jLabel5;
	private JTextPane minFromPane;
	private JLabel hou;
	private JTextPane hourFromPane;
	private JLabel jLabel4;
	private JLabel jLabel3;
	private JButton cancelButton;
	private JTextPane datePane;
	private Request request;
	private Calendar date;
	private RequestP reqP;
	private model.search.System sistem;
	private boolean isModify = false;
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new SingleTFPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public SingleTFPanel() {
		// TODO Auto-generated constructor stub
	}
	
	public SingleTFPanel(Request r, RequestP rp, model.search.System sis) {
		super();
		this.request = r;
		this.reqP = rp;
		this.sistem = sis;
		if(reqP.isModify()){
			this.date = r.getTimeFrames().get(0).getDateStart();
			this.isModify = true;
		}
		initGUI();
	}
	
	public void setDate(Calendar c){
		this.date = c;
	}
	
	public void setDisabled(){
		cancelButton.setEnabled(false);
		datePickerButton.setEnabled(false);
		okButton.setEnabled(false);
		jLabel1.setEnabled(false);
		jLabel2.setEnabled(false);
		jLabel3.setEnabled(false);
		jLabel4.setEnabled(false);
		jLabel5.setEnabled(false);
		jLabel6.setEnabled(false);
		jLabel7.setEnabled(false);
		hou.setEnabled(false);
		datePane.setEnabled(false);
		minFromPane.setEnabled(false);
		minsToPane.setEnabled(false);
		hourFromPane.setEnabled(false);
		hoursToPane.setEnabled(false);
		
		
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setLayout(null);
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setBounds(12, 12, 207, 14);
				jLabel1.setName("jLabel1");
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setBounds(12, 48, 106, 14);
				jLabel2.setName("jLabel2");
			}
			{
				datePane = new JTextPane();
				this.add(datePane);
				datePane.setBounds(115, 48, 109, 20);
				datePane.setName("datePane");
			}
			{
				datePickerButton = new JButton();
				this.add(datePickerButton);
				datePickerButton.setBounds(247, 48, 110, 21);
				datePickerButton.setName("datePickerButton");
				datePickerButton.addActionListener(new OpenDatePicker(this, datePane));
			}
			{
				cancelButton = new JButton();
				this.add(cancelButton);
				cancelButton.setBounds(188, 259, 82, 21);
				cancelButton.setName("cancelButton");
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setBounds(12, 96, 140, 14);
				jLabel3.setName("jLabel3");
			}
			{
				jLabel4 = new JLabel();
				this.add(jLabel4);
				jLabel4.setBounds(82, 133, 76, 14);
				jLabel4.setName("jLabel4");
			}
			{
				hourFromPane = new JTextPane();
				this.add(hourFromPane);
				hourFromPane.setBounds(114, 93, 61, 20);
				hourFromPane.setName("hourFromPane");
			}
			{
				hou = new JLabel();
				this.add(hou);
				hou.setBounds(175, 96, 39, 14);
				hou.setName("hou");
			}
			{
				minFromPane = new JTextPane();
				this.add(minFromPane);
				minFromPane.setBounds(214, 93, 64, 20);
				minFromPane.setName("minFromPane");
			}
			{
				jLabel5 = new JLabel();
				this.add(jLabel5);
				jLabel5.setBounds(278, 96, 67, 14);
				jLabel5.setName("jLabel5");
			}
			{
				hoursToPane = new JTextPane();
				this.add(hoursToPane);
				hoursToPane.setBounds(114, 130, 61, 20);
			}
			{
				minsToPane = new JTextPane();
				this.add(minsToPane);
				minsToPane.setBounds(214, 130, 64, 20);
				minsToPane.setName("minsToPane");
			}
			{
				jLabel6 = new JLabel();
				this.add(jLabel6);
				jLabel6.setName("jLabel6");
				jLabel6.setBounds(176, 133, 39, 14);
			}
			{
				jLabel7 = new JLabel();
				this.add(jLabel7);
				jLabel7.setName("jLabel7");
				jLabel7.setBounds(278, 133, 67, 14);
			}
			{
				okButton = new JButton();
				this.add(okButton);
				okButton.setBounds(302, 259, 82, 21);
				okButton.setName("okButton");
				okButton.addActionListener(new CreateSingleRequest(this));
			}
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private class OpenDatePicker implements ActionListener{

		private SingleTFPanel stfp;
		private JTextPane jtp;
		
		public OpenDatePicker(SingleTFPanel s, JTextPane t){
			this.stfp = s;
			this.jtp = t;
		}
		
		public void actionPerformed(ActionEvent e) {
			new DatePicker(stfp, jtp);
			
		}
		
	}

	public JButton getCancelButton() {
		return cancelButton;
	}

	private class CreateSingleRequest implements ActionListener{
		private SingleTFPanel stfp;
		
		public CreateSingleRequest(SingleTFPanel t){
			this.stfp = t;
		}
		
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if((!(hourFromPane.getText().equals("")))&& (!(hoursToPane.getText().equals(""))) &&
					(!(datePane.getText().equals("Pick a date"))) &&
					(!(minFromPane.getText().equals(""))) && (!(minsToPane.getText().equals("")))){
				request.getTimeFrames().get(0).setDateStart(date);
				request.getTimeFrames().get(0).setTimeStart(new GregorianCalendar(1900, Calendar.JANUARY, 1, Integer.parseInt(hourFromPane.getText()), Integer.parseInt(minFromPane.getText())));
				request.getTimeFrames().get(0).setTimeEnd(new GregorianCalendar(1900, Calendar.JANUARY, 1, Integer.parseInt(hoursToPane.getText()), Integer.parseInt(minsToPane.getText())));
				stfp.setDisabled();
				reqP.getTabs().setEnabledAt(0, false);
				reqP.getTabs().setEnabledAt(1, false);
				reqP.getCreateButton().setEnabled(true);
				
				if(!(isModify)){
					sistem.getReqs().add(request);
					sistem.getRequestTotalGeneralList().add(request);
					request.setId(sistem.getRequestTotalGeneralList().size());
				}
				
			}
			
			else{
				new NoSelectedThingsDialog();
			}
		}
		
	}
	
	public void modifyRequest(Request r){
		this.datePane.setText(r.getTimeFrames().get(0).getStringDateStart());
		this.hourFromPane.setText(r.getTimeFrames().get(0).getStringHourStart());
		this.hoursToPane.setText(r.getTimeFrames().get(0).getStringHourEnd());
		this.minFromPane.setText(r.getTimeFrames().get(0).getStringMinStart());
		this.minsToPane.setText(r.getTimeFrames().get(0).getStringMinEnd());
	}
	
}
